package com.qumap.edititem.update;

import com.qumap.edititem.EditItemDisplay;
import com.qumap.edititem.EditItemInfo;
import com.qumap.edititem.update.callback.ChangeCallback;
import com.qumap.edititem.update.callback.DeleteCallback;
import com.qumap.edititem.update.callback.SaveCallback;
import com.qumap.edititem.update.callback.UpdateCallback;
import com.qumap.item.ItemOnMap;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 28.05.11
 * Time: 9:55
 * To change this template use File | Settings | File Templates.
 */
public class ItemUpdater {

    private EditItemDisplay display;


    public ItemUpdater(EditItemDisplay display) {
        this.display = display;
    }

    public void save(ItemOnMap item) {
        IItemUpdateProcessorAsync item_update_processor = getProcessor(item);
        ChangeCallback item_save_callback = new SaveCallback();
        initChangeCallback(item_save_callback, item);
        item_update_processor.save(item.getContent(), item_save_callback);
    }

    public void update(ItemOnMap item) {
        IItemUpdateProcessorAsync item_update_processor = getProcessor(item);
        ChangeCallback item_update_callback = new UpdateCallback();
        initChangeCallback(item_update_callback, item);
        item_update_processor.update(item.getContent(), item_update_callback);
    }

    public void delete(ItemOnMap item) {
        IItemUpdateProcessorAsync item_update_processor = getProcessor(item);
        ChangeCallback item_delete_callback = new DeleteCallback();
        initChangeCallback(item_delete_callback, item);
        item_update_processor.delete(item.getContent().getId(), item_delete_callback);
    }

    private IItemUpdateProcessorAsync getProcessor(ItemOnMap item) {
        return ((EditItemInfo)item.getItemInfo()).getItemChanger();
    }

    private void initChangeCallback(ChangeCallback callback, ItemOnMap item_to_change) {
        callback.setItem(item_to_change);
        callback.setDisplay(display);
        callback.init();
    }

}